package com.mix;

import com.aspose.words.*;
import com.aspose.words.Shape;

import java.awt.*;

/**
 * 插入一个水印和多个水印
 */
public class WordWatermarkUtil {
    /**
     * 插入一个水印摆放正中间
     * @param watermarkText 水印内容
     * @param doc Document对象
     */
    public static void insertWatermarkText(Document doc, String watermarkText) {
        Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
        watermark.getTextPath().setText(watermarkText);//水印内容
        watermark.getTextPath().setFontFamily("宋体");
        try {
            //具体字体内容大小进行自定义设置
            watermark.setWidth(500);//水印宽度
            watermark.setHeight(100);//水印高度
        } catch (Exception e) {
            e.printStackTrace();
        }
        watermark.setRotation(-40);//旋转水印
        watermark.getFill().setColor(Color.lightGray); //填充颜色(没有填充颜色字会变成空心的)
        watermark.setStrokeColor(Color.lightGray);//水印颜色 浅灰色
        watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);//设置相对水平位置
        watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);//设置相对垂直位置
        watermark.setWrapType(WrapType.NONE);//设置包装类型
        watermark.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直对齐
        watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置文本水平对齐
        Paragraph watermarkPara = new Paragraph(doc);
        watermarkPara.appendChild(watermark);
        for (Section sect : doc.getSections()) {
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
        }
    }

    /**
     * 插入多个水印
     * @param watermarkText 水印内容
     * @param doc Document对象
     */
    public static void insertMoreWatermarkText(Document doc, String watermarkText) {
        Paragraph watermarkPara = new Paragraph(doc);
        //循环添加水印，这里横纵都在一条水平线，不太好看，可以对i和j控制使之有适当位移
        for (int j = -30; j < 700; j = j + 200) {
            for (int i = 50; i < 500; i = i + 100) {
                Shape waterShape = ShapeMore(doc, watermarkText, j, i);
                watermarkPara.appendChild(waterShape);
            }
        }
        for (Section sect : doc.getSections()) {
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
        }
    }

    /**
     * @param doc 操作的文档对象
     * @param watermarkText 水印内容
     * @param left 距离左边的距离
     * @param top 距离顶部的距离
     * @return
     */
    public static Shape ShapeMore(Document doc, String watermarkText, double left, double top){
        Shape waterShape = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
        //水印内容
        waterShape.getTextPath().setText(watermarkText);
        //水印字体
        waterShape.getTextPath().setFontFamily("楷体");
        try {
            //具体字体内容大小进行自定义设置
            waterShape.setWidth(136);//水印宽度
            waterShape.setHeight(18);//水印高度
        } catch (Exception e) {
            e.printStackTrace();
        }
        //旋转水印
        waterShape.setRotation(-40);
        //填充颜色(没有填充颜色字会变成空心的)
        waterShape.getFill().setColor(new Color(217,217,217));
        //水印颜色 浅灰色
        waterShape.setStrokeColor(new Color(217,217,217));
        //将水印放置在页面中心
        waterShape.setLeft(left);
        waterShape.setTop(top);
        //设置包装类型
        waterShape.setWrapType(WrapType.NONE);
        return waterShape;
    }

    /**
     * 插入水印
     * @param watermarkPara 水印段落
     * @param sect 部件
     * @param headerType 头标类型字段
     */
    private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) {
        HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
        if (header == null) {
            header = new HeaderFooter(sect.getDocument(), headerType);
            sect.getHeadersFooters().add(header);
        }
        header.appendChild(watermarkPara.deepClone(true));
    }
}